package inet.app.vod.view;

import inet.app.vod.dialog.MySpinnerDialogFragment;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;

public class MySpinner extends RelativeLayout implements
		MySpinnerDialogFragment.SpinnerListener {

	private MySpinnerDialogFragment mSpinnerDialog;
	private FragmentActivity mActivity;

	private String mTitle;
	private String[] mObjects;
	private MySpinerItemClickListener mItemListener;
	private int mItemSelect = 0;

	public MySpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MySpinner(Context context) {
		super(context);
		init(context);
	}

	private void init(Context context) {
		this.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != mSpinnerDialog) {
					mSpinnerDialog.setCurPosition(mItemSelect);
					mSpinnerDialog.show(mActivity.getSupportFragmentManager(),
							"spinner");
				}
			}
		});
	}

	public void initData(FragmentActivity activity, String title,
			String[] objects) {
		this.mTitle = title;
		this.mObjects = objects;
		this.mActivity = activity;
		mSpinnerDialog = MySpinnerDialogFragment.newInstance(mTitle, mObjects);
		mSpinnerDialog.setSpinnerListener(this);
	}

	public int getItemSelect() {
		return mItemSelect;
	}

	public void setItemSelect(int itemSelect) {
		mItemSelect = itemSelect;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {

		mItemSelect = position;
		if (null != mSpinnerDialog) {
			mSpinnerDialog.dismiss();
		}
		if (null != mItemListener) {
			mItemListener.onItemClick(position);
		}
	}

	public void setItemClickListener(MySpinerItemClickListener listener) {
		this.mItemListener = listener;
	}

	public interface MySpinerItemClickListener {
		public void onItemClick(int position);
	}

}
